package com.qeegoo.b2bautozityp.moudle.webview;

import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.databinding.ActivityWebviewBinding;
import com.qeegoo.b2bautozityp.injector.component.DaggerWebViewComponent;
import com.qeegoo.b2bautozityp.moudle.base.AppBar;
import com.qeegoo.b2bautozityp.moudle.base.BaseActivity;
import com.qeegoo.b2bautozityp.moudle.webview.viewmodel.WebViewViewModel;

import javax.inject.Inject;

/**
 * Created by lining on 2017/9/27.
 */

public class WebViewActivity extends BaseActivity<ActivityWebviewBinding> {

    public static class Extra{
        public static final String kIn_title = "title";//页面 title
        public static final String kIn_url = "url";//页面 url
    }
    @Inject
    WebViewViewModel mViewModel;

    private String url;
    private WebSettings webSettings;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initInjector() {
        DaggerWebViewComponent.builder().appComponent(getAppComponent()).build().inject(this);
    }

    @Override
    protected void initViews() {
        String sTitle = getIntent().getExtras().getString(Extra.kIn_title);
        url = getIntent().getExtras().getString(Extra.kIn_url);
        initToolBar(mBinding.layoutToolbar, new AppBar(sTitle, true));
        mBinding.setViewModel(mViewModel);
        if (!TextUtils.isEmpty(url)) {
            mBinding.webView.loadUrl(url);
        }
        mBinding.webView.setFocusable(true);

        mBinding.webView.setWebChromeClient(new WebChromeClient());
        webSettings = mBinding.webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setDefaultTextEncodingName("utf-8");
        WebViewClient client = new WebViewClient();
        mBinding.webView.setWebViewClient(client);
    }
}
